go
CREATE PROC sp_Select_ChiTietKhachHangTraLaiBanBuon
	@HanhDong nvarchar(50),
	@MaKhachHangTraLai varchar(50)
AS 
BEGIN
	IF @HanhDong=N'1'
	BEGIN
		Select * From ChiTietKhachHangTraLaiBanBuon 
		Where MaKhachHangTraLai=@MaKhachHangTraLai and Deleted=N'False'
	END
	IF @HanhDong=N'2'
	BEGIN
		Select * From ChiTietKhachHangTraLaiBanBuon Where Deleted=N'False'
	END
END
go

-- insert update khach hang tra lai ban buon
USE [QuanLySieuThi]
GO
/****** Object:  StoredProcedure [dbo].[sp_InsertUpdate_KhachHangTraLaiBanBuon]    Script Date: 03/20/2012 16:53:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[sp_InsertUpdate_KhachHangTraLaiBanBuon]
	@HanhDong nvarchar(50),
	@MaKhachHangTraLaiBanBuonID int,
	@MaKhachHangTraLaiBanBuon varchar(50),
	@NgayNhap	Datetime,
	@MaKhachHang	varchar(50),
	@TenKhachHang nvarchar(200),
	@DuNo float,
	@NguoiTra	nvarchar(200),
	@MaKho	varchar(50),
	@TenKho  nvarchar(200),
	@MaNhanVien	varchar(50),
	@TenNhanVien	nvarchar(200),
	@MaTienTe	varchar(50),
	@TyGiaTienTe	float,
	@Thue float,
	@TienBoiThuong	float,
	@ThanhToanNgay	float,
	@ThanhToanSauKhiLapPhieu float,
	@GhiChu	nvarchar(200),
	@Deleted	bit
AS
BEGIN
	IF @HanhDong='Them'
	BEGIN
		Insert Into KhachHangTraLaiBanBuon
		Values(@MaKhachHangTraLaiBanBuon,
				@NgayNhap,
				@MaKhachHang,
				@TenKhachHang,
				@DuNo,
				@NguoiTra,
				@MaKho,
				@TenKho ,
				@MaNhanVien,
				@TenNhanVien,
				@MaTienTe,
				@TyGiaTienTe,
				@Thue,
				@TienBoiThuong,
				@ThanhToanNgay,
				@ThanhToanSauKhiLapPhieu,
				@GhiChu,
				@Deleted)
	END
END

----- insert update chi tiet khach hang tra lai ban buon
USE [QuanLySieuThi]
GO
/****** Object:  StoredProcedure [dbo].[sp_ChiTietKhachHangTraLaiBanBuon]    Script Date: 03/20/2012 16:54:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_ChiTietKhachHangTraLaiBanBuon]
	@HanhDong varchar(20),
	@MaKho varchar(50),
	@MaKhachHangTraLai varchar(50),
	@MaHoaDonBanBuon	varchar(50),
	@MaHangHoa	varchar(50),
	@TenHangHoa	nvarchar(200),
	@SoLuong	float,
	@PhanTramChietKhau float,
	@PhanTramChietKhauThuongMai	float,
	@DonGia	float,
	@ThanhTien	float,
	@Thue float,
	@GhiChu	nvarchar(200),
	@Deleted	bit
AS
BEGIN
	IF @HanhDong = 'Insert'
	BEGIN
		IF ((Select COUNT(*) From ChiTietKhachHangTraLaiBanBuon Where MaKhachHangTraLai=@MaKhachHangTraLai AND MaHoaDonBanBuon=@MaHoaDonBanBuon AND MaHangHoa=@MaHangHoa AND Deleted=N'False')<=0)
		BEGIN
			INSERT INTO ChiTietKhachHangTraLaiBanBuon VALUES(
			@MaKhachHangTraLai,
			@MaHoaDonBanBuon,
			@MaHangHoa,
			@TenHangHoa,
			@SoLuong,
			@PhanTramChietKhau,
			@PhanTramChietKhauThuongMai,
			@DonGia,
			@ThanhTien,
			@Thue,
			'2012-01-01 00:00:00',
			@GhiChu,
			@Deleted)
			
			--IF @@ERROR=0
			--BEGIN
			--	exec sp_CongKho @MaKho,@MaHangHoa,@SoLuong
			--END
		END
		ELSE
		BEGIN
			Update ChiTietKhachHangTraLaiBanBuon
			Set SoLuong=@SoLuong,
				PhanTramChietKhau=@PhanTramChietKhau,
				PhanTramChietKhauThuongMai=@PhanTramChietKhauThuongMai,
				DonGia=@DonGia,
				ThanhTien=@ThanhTien,
				Thue=@Thue
			Where MaKhachHangTraLai=@MaKhachHangTraLai AND MaHoaDonBanBuon=@MaHoaDonBanBuon AND MaHangHoa=@MaHangHoa AND Deleted=N'False'
			--IF @@ERROR=0
			--BEGIN
			--	exec sp_CongKho @MaKho,@MaHangHoa,@SoLuong
			--END
		END
	END
END